package com.yuchen.ishop.goods;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.ishop.entity.Goods;
import com.yuchen.ishop.entity.GoodsImg;
import com.yuchen.jaxrs.ResponseUtils;
import com.yuchen.jaxrs.Responses;
import com.yuchen.utils.fastjson.JsonUtils;
import org.apache.log4j.Logger;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Transactional
@Path("goodsImgService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class GoodsImgServiceImpl implements GoodsImgService {

    private final static Logger LOG = Logger.getLogger(GoodsImgService.class);

    @Autowired
    private BaseDao dao;

    @GET
    @Path("get/{id}")
    @Transactional(readOnly = true)
    public GoodsImg get(@PathParam("id") Long id) {
        return dao.fetch(GoodsImg.class, id);
    }

    @POST
    @Path("insert")
    public Responses insert(GoodsImg goodsImg) {
        goodsImg = dao.insert(goodsImg);
        Responses responses = ResponseUtils.success();
        responses.setJson(JsonUtils.toJSON(goodsImg));
        return responses;
    }

    @DELETE
    @Path("delete/{id}")
    public Responses delete(@PathParam("id") Long id) {
        dao.delete(GoodsImg.class, id);
        return ResponseUtils.success();
    }

    @POST
    @Path("changeType")
    public Responses changeType(Map params) {
        if (params.get("allIds") != null) {
            String allIds = (String) params.get("allIds");
            dao.update(GoodsImg.class, Chain.make("type", 0), Cnd.where("id", "in", allIds.split(",")));
        }
        dao.update(GoodsImg.class, Chain.make("type", 1), Cnd.where("id", "=", params.get("id")));
        return ResponseUtils.success();
    }

    @GET
    @Path("find/{goodsId}")
    public Responses findByGoodsId(@PathParam("goodsId") Long goodsId) {
        Responses responses = ResponseUtils.success();
        List<GoodsImg> goodsImgs = dao.query(GoodsImg.class, Cnd.where("goodsId", "=", goodsId));
        responses.setJson(JsonUtils.toJSON(goodsImgs));
        return responses;
    }

    @GET
    @Path("getMainImg/{goodsId}")
    public String getMainImg(@PathParam("goodsId") Long goodsId) {
        GoodsImg goodsImg = dao.fetch(GoodsImg.class, Cnd.where("goodsId", "=", goodsId).and("type", "=", 1));
        if (goodsImg != null)
            return goodsImg.getImageUrl();
        return null;
    }

    @DELETE
    @Path("deleteByGoodsId/{goodsId}")
    public Responses deleteByGoodsId(@PathParam("goodsId") Long goodsId) {
        Responses responses = ResponseUtils.success();
        dao.clear(GoodsImg.class, Cnd.where("goodsId", "=", goodsId));
        return responses;
    }
}
